using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;

namespace InfoJetSoft.Service.Util
{
	/// <summary>
	/// Summary description for HttpValue.
	/// </summary>
	public class FormConfigContextValue
	{
		public static string Context = "context";
		public static string Request = "request";
		public static string Session = "session";
		public static string Application = "application";

		private string scope = null;
		private string key = null;
		private string path = null;
		private bool openTemplate = true;
		private bool openXml = true;

		public FormConfigContextValue()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public string Scope
		{
			get
			{
				return this.scope;
			}
			set
			{
				this.scope = value;
			}
		}

		public string Key
		{
			get
			{
				return this.key;
			}
			set
			{
				this.key = value;
			}
		}

		public string Path
		{
			get
			{
				return this.path;
			}
			set
			{
				this.path = value;
			}
		}

		public bool OpenTemplate
		{
			get
			{
				return this.openTemplate;
			}
			set
			{
				this.openTemplate = value;
			}
		}

		public bool OpenXml
		{
			get
			{
				return this.openXml;
			}
			set
			{
				this.openXml = value;
			}
		}

		public static void UpdateWhileOpenTemplate( Form form )
		{
			if( form.Manifest.FormConfiguration.ContextValueList != null )
			{
				foreach( FormConfigContextValue contextValue in form.Manifest.FormConfiguration.ContextValueList )
				{
					if( contextValue.OpenTemplate )
					{
						updateValue( form, contextValue );
					}
				}
			}
		}

		public static void UpdateWhileOpenXml( Form form )
		{
			if( form.Manifest.FormConfiguration.ContextValueList != null )
			{
				foreach( FormConfigContextValue contextValue in form.Manifest.FormConfiguration.ContextValueList )
				{
					if( contextValue.OpenXml )
					{
						updateValue( form, contextValue );
					}
				}
			}
		}

		private static void updateValue( Form form, FormConfigContextValue contextValueDef )
		{
			XmlNodeList nodeList = DOMUtil.selectNodes( form.XmlDoc.DocumentElement, contextValueDef.Path, 0, form.XmlNsResolver );
			foreach( XmlNode node in nodeList )
			{
				if( contextValueDef.Scope.Equals( FormConfigContextValue.Application ) )
				{
					if( form.HttpContext.Session != null )
					{
						string applicationValue = (string)form.HttpContext.Application[ contextValueDef.Key ];
						if( applicationValue != null )
						{
							DOMUtil.setNodeValue( node, applicationValue );
						}
					}
				}
				if( contextValueDef.Scope.Equals( FormConfigContextValue.Session ) )
				{
					if( form.HttpContext.Session != null )
					{
						string sessionValue = (string)form.HttpContext.Session[ contextValueDef.Key ];
						if( sessionValue != null )
						{
							DOMUtil.setNodeValue( node, sessionValue );
						}
					}
				}
				else if( contextValueDef.Scope.Equals( FormConfigContextValue.Request ) )
				{
					string requestValue = (string)form.HttpContext.Request[ contextValueDef.Key ];
					if( requestValue != null )
					{
						DOMUtil.setNodeValue( node, requestValue );
					}
				}
				else if( contextValueDef.Scope.Equals( FormConfigContextValue.Context ) )
				{
					Hashtable variables = form.JetContext.Variables;
					if( variables != null )
					{
						string contextValue = (string)variables[ contextValueDef.Key ];
						if( contextValue != null )
						{
							DOMUtil.setNodeValue( node, contextValue );
						}
					}
				}
			}
		}

	}
}
